package com.axend.aerosense.main.config;

import android.content.Context;

import com.alibaba.android.arouter.launcher.ARouter;
import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.tencent.android.tpush.XGIOperateCallback;
import com.tencent.android.tpush.XGPushConfig;
import com.tencent.android.tpush.XGPushManager;
import com.axend.aerosense.base.bean.BaseDevInfoBean;
import com.axend.aerosense.base.bean.BaseStringBean;
import com.axend.aerosense.base.storage.MmkvHelper;
import com.axend.aerosense.common.UrlApi;
import com.axend.aerosense.common.config.Gobal;
import com.axend.aerosense.common.router.RouterActivityPath;
import com.axend.aerosense.common.util.JsonUtil;
import com.axend.aerosense.main.R;
import com.axend.aerosense.network.EasyHttp;
import com.axend.aerosense.network.cache.model.CacheMode;
import com.axend.aerosense.network.callback.SimpleCallBack;
import com.axend.aerosense.network.exception.ApiException;

/**
 * 腾讯TPNS推送配置
 * Author: ChenHongWei
 * Date: 2021/3/1 13:54
 */
public class TpPushConfig {

    private Context context;

//    private final static String XIAOMI_APP_ID = "2882303761519013616";
//    private final static String XIAOMI_APP_KEY = "5671901364616";

//    private final static String MEIZU_APP_ID = "139359";
//    private final static String MEIZU_APP_KEY = "38de1b24894f4262a0fd1124c395114a";

//    private final static String OPPO_APP_KEY = "be4286aee894485d8915478612bddb7b";
//    private final static String OPPO_APP_SECRET = "357005a21469489ab30c5d2b48eb4755";

    public TpPushConfig(Context context){
        this.context = context;
    }

    public void init(){
        //TPNS推送配置
        XGPushConfig.enableDebug(context,true);
        //小米
//        XGPushConfig.setMiPushAppId(context, XIAOMI_APP_ID);
//        XGPushConfig.setMiPushAppKey(context, XIAOMI_APP_KEY);
//        //魅族
//        XGPushConfig.setMzPushAppId(context, MEIZU_APP_ID);
//        XGPushConfig.setMzPushAppKey(context, MEIZU_APP_KEY);
//        //OPPO
//        XGPushConfig.setOppoPushAppId(context, OPPO_APP_KEY);
//        XGPushConfig.setOppoPushAppKey(context, OPPO_APP_SECRET);
        //打开第三方推送
        XGPushConfig.enableOtherPush(context,true);
        XGPushManager.registerPush(context, new XGIOperateCallback() {
            @Override
            public void onSuccess(Object data, int flag) {
                //token在设备卸载重装的时候有可能会变
                LogUtils.d("TPush", "注册成功，设备token为：" + data);
                //System.out.println("TPush，注册成功，设备token为：" + data);
                String pushToken = data.toString();
                sendPushToken(pushToken);
            }

            @Override
            public void onFail(Object data, int errCode, String msg) {
                LogUtils.d("TPush", "注册失败，错误码：" + errCode + ",错误信息：" + msg);
            }
        });
    }

    /** 发送PushToken到服务器 */
    public void sendPushToken(String pushToken){
        EasyHttp.post(UrlApi.URL.SetPushToken.getUrl())
                .headers("token", MmkvHelper.getInstance().getMmkv().decodeString(Gobal.SAVE_KEY.TOKEN))
                .headers("pushToken", pushToken)
                .upJson(JsonUtil.toJson(new BaseDevInfoBean()))
                .cacheMode(CacheMode.NO_CACHE)
                .execute(new SimpleCallBack<BaseStringBean>() {
                    @Override
                    public void onTokenExpire() {
                        ToastUtils.showShort(R.string.common_login_first);
                        //登录界面不做处理
                        ARouter.getInstance().build(RouterActivityPath.Login.PAGE_LOGIN_HOME).withBoolean("jumpMainPage", true).greenChannel().navigation();
                    }

                    @Override
                    public void onError(ApiException e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(BaseStringBean baseStringBean) {
                        LogUtils.d("发送PushToken到服务器成功！");
                    }
                });
    }
}
